package com.zone.lib.download.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.zone.lib.download.DownloadConstant;
import com.zone.lib.download.DownloadStatus;
import com.zone.lib.download.bean.DownloadInfo;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk_api.ApiManager;
import com.zone.vehicle.sdk.module.superapp.entity.SyncStatusEntity;
import com.zone.vehicle.sdk.module.superapp.request.SyncStatusRequest;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/20 21:57
 * @Description:
 */
public class AppStatusUpdateNotify {
    private static final String TAG = AppStatusUpdateNotify.class.getName();


    public static void appNotify(Context context, DownloadInfo downloadInfo, Download mDownload){
        if (ObjectUtils.isEmpty(downloadInfo)){
            return;
        }
        Log.d(TAG, "downloadInfo = "+ JSON.toJSONString(downloadInfo));
        Log.d(TAG, "download = "+ JSON.toJSONString(mDownload));
        if (ObjectUtils.isNotEmpty(downloadInfo)){
            if (ObjectUtils.isEmpty(mDownload)){
                //本地存储
                Download download = new Download();
                download.setCode(downloadInfo.getCode());
                download.setSize(downloadInfo.getSize());
                download.setDownloadLocation(0);
                download.setDownloadStatus(DownloadStatus.PREPARE_DOWNLOAD);
                download.setDownloadUrl(downloadInfo.getDownloadUrl());
                if (ObjectUtils.isNotEmpty(downloadInfo.getFile())){
                    download.setFilePath(downloadInfo.getFile().getAbsolutePath());
                }
                download.setVersion(downloadInfo.getVersion());
                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
                mDownload = download;
            }else {
                if (ObjectUtils.isEmpty(mDownload.getId())){
                    Download temp = DownloadDaoUtilsStore.getInstance().getDownloadByCode(mDownload.getCode());
                    if (ObjectUtils.isNotEmpty(temp)){
                        mDownload.setId(temp.getId());
                    }
                }
//                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().update(mDownload);
            }

            //发广播
            Intent intent = new Intent();
            intent.setAction(DownloadConstant.DOWNLOAD_APP_ACTION);
            intent.putExtra(DownloadConstant.EXTRA_INTENT_DOWNLOAD, mDownload);
            context.sendBroadcast(intent);
            //上报状态
            int process = 0;
            if (mDownload.getSize() > 0) {
                process = (int) ((mDownload.getDownloadLocation() * 100) / mDownload.getSize());
            }
//            AppStatusUpdateNotify.sendAppState(mDownload.getAppType(), downloadInfo.getVin(), downloadInfo.getUserId(), mDownload.getCode(), mDownload.getDownloadStatus(), ""+process, mDownload.getVersion());
        }
    }

    public static void sendAppState(String type, String vin, String userId, String code, int state, String process, String version){
        SyncStatusEntity entity = new SyncStatusEntity();
        entity.setCode(code);
        entity.setProcess(process);
        entity.setVersion(version);
        entity.setState(state);
        entity.setTime(System.currentTimeMillis());
        uploadAppStatus(type, vin, userId, entity);
    }

    public static void uploadAppStatus(String type, String vin, String userId, SyncStatusEntity entity){
        if (ObjectUtils.isEmpty(entity)){
            return;
        }
        Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(entity.getCode());
        if (ObjectUtils.isEmpty(download)){
            return;
        }
        SyncStatusRequest request = new SyncStatusRequest();
        request.setType(type);
        request.setVin(vin);
        request.setUserId(userId);
        List<SyncStatusEntity> statusList = new ArrayList<>();
        statusList.add(entity);
        request.setStatusList(statusList);
        ApiManager.getInstance().syncStatus(request, new ResultCallBack<Result>() {
            @Override
            public void onSuccess(Result result) {
                Log.d(TAG, result.toString());
            }

            @Override
            public void onError(String errorCode, String message) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
